package com.chancingpack.shiro.realm;

import com.chancingpack.shiro.PasswordHelper;
/*import com.xqls.bean.LoginBean;
import com.xqls.bean.UserBean;
import com.xqls.dal.model.Login;
import com.xqls.dal.model.User;
import com.xqls.redis.Constants;
import com.xqls.redis.RedisCache;
import com.xqls.redis.RedisKeyEnum;
import com.xqls.service.LoginService;
import com.xqls.service.UserService;
import com.xqls.shiro.PasswordHelper;
import com.xqls.util.ObjectUtil;*/
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 
 * @author SCJ
 * 获得用户的权限和凭证
 * 处理redis的缓存
 */
public class LoginRealm /*extends AuthorizingRealm implements Realm, InitializingBean*/ {
/*

	@Autowired private UserService userService;
	
	@Autowired private LoginService loginService;

	@Autowired
	private RedisCache<String, Object> redisCache;

	public LoginRealm() {

	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
	}

	*/
/**
	 * 获得用户的权限，把结果放入redis的缓存中
	 *//*

	@SuppressWarnings("unchecked")
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		Set<String> roleStringSet = null;
		Set<String> permStringSet = null;
		List<Login> loginList =null;
		Map<String, Object> map = new HashMap<String, Object>();
		
		String userName = (String) super.getAvailablePrincipal(principals);

		// 去缓存中查询 角色和权限;
		roleStringSet = (Set<String>) redisCache.get(Constants.getRedisKey(RedisKeyEnum.USER_ROLES, userName));
		permStringSet = (Set<String>) redisCache.get(Constants.getRedisKey(RedisKeyEnum.USER_PERMS, userName));

		// 缓存为空去数据库中查询
		if (roleStringSet == null || permStringSet == null) {
			roleStringSet = new HashSet<String>();
			permStringSet = new HashSet<String>();
			
			map.put("userName", userName);

			loginList = this.loginService.getLoginByMap(map);
			for (Login login : loginList) {
				roleStringSet.add(login.getRole().getRoleId());
				if(StringUtils.isNotEmpty(login.getPermission().getUrl())){
					permStringSet.add(login.getPermission().getUrl());
				}
			}
			
			redisCache.put(Constants.getRedisKey(RedisKeyEnum.USER_ROLES, userName), roleStringSet);
			redisCache.put(Constants.getRedisKey(RedisKeyEnum.USER_PERMS, userName), permStringSet);
		}
		
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		
		info.setRoles(roleStringSet);
		info.setStringPermissions(permStringSet);

		return info;
	}

	*/
/**
	 * 获得用户的认证，把结果放入redis的缓存中
	 *//*

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)throws AuthenticationException{
		UsernamePasswordToken upToken = (UsernamePasswordToken) token;
		String userName = upToken.getUsername();
        String password=String.valueOf(upToken.getPassword());
        
        List<User> userList = this.userService.getUserListForLogin(userName, password);
        User user=null;
        if(userList.isEmpty())
        {
        	throw new AuthenticationException("账户密码不正确！");
        }
        else
        {
        	user=userList.get(0);
        }
        
        LoginBean loginBean=new LoginBean();
        loginBean.setUser(user);
        
        PasswordHelper.encryptPassword(loginBean);

		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(loginBean.getUser().getUserName(),
				loginBean.getUser().getPassword(),
				ByteSource.Util.bytes(loginBean.getCredentialsSalt()),
				super.getName());
		
		UserBean userBean=(UserBean)ObjectUtil.transfer(user,UserBean.class);

		redisCache.put(Constants.getRedisKey(RedisKeyEnum.USER_SESSION, userName),userBean);

		return info;
	}

	*/
/**
	 * 从redis的缓存中，移除指定用户的信息
	 * @param principal
	 *//*

	public void clearCachedAuthorizationInfo(String principal){
		SimplePrincipalCollection pc=new SimplePrincipalCollection(principal,super.getName());
		super.clearCachedAuthorizationInfo(pc);
		super.clearCachedAuthenticationInfo(pc);
		super.clearCache(pc);
		
		redisCache.remove(Constants.getRedisKey(RedisKeyEnum.USER_ROLES, principal));
		redisCache.remove(Constants.getRedisKey(RedisKeyEnum.USER_PERMS, principal));
		redisCache.remove(Constants.getRedisKey(RedisKeyEnum.USER_SESSION, principal));
	}
	
	*/
/**
	 * 从redis的缓存中，移除所有用户的信息
	 *//*

	public void clearAllCachedAuthorizationInfo(){
		redisCache.clear();
	}
*/
}
